﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using ComLib.ConfigService;
using ComLib.ComType;
using ComLib.ComFun;
using LogLib;
using System.IO;

namespace FTPService
{
    [XmlRootAttribute("FTPOption", Namespace = "FTPService", IsNullable = false)]
    public class FTPOption : XmlSerializeBase
    {
        /// <summary>
        /// 主机IP
        /// </summary>
        private string _ip;
        [XmlElementAttribute("ip", IsNullable = true)]
        public string IP
        {
            get { return _ip; }
            set { _ip = value; }
        }

        /// <summary>
        /// 端口号
        /// </summary>
        private int _port = 21;
        [XmlElementAttribute("port", IsNullable = false)]
        public int Port
        {
            get { return _port; }
            set { _port = value; }
        }

        /// <summary>
        /// 默认目录
        /// </summary>
        private string _folder;
        [XmlElementAttribute("folder", IsNullable = true)]
        public string Folder
        {
            get { return _folder; }
            set { _folder = value; }
        }

        /// <summary>
        /// 用户名
        /// </summary>
        private string _user;
        [XmlElementAttribute("user", IsNullable = true)]
        public string User
        {
            get { return _user; }
            set { _user = value; }
        }

        /// <summary>
        /// 登录密码
        /// </summary>
        private string _ciphertext;
        [XmlElementAttribute("pwd", IsNullable = true)]
        public string Ciphertext
        {
            get { return _ciphertext; }
            set { _ciphertext = value; }
        }

        /// <summary>
        /// 上传成功是否是必要的
        /// </summary>
        private bool _isNecessary = true;
        [XmlElementAttribute("isNecessary", IsNullable = false)]
        public bool IsNecessary
        {
            get { return _isNecessary; }
            set { _isNecessary = value; }
        }

        /// <summary>
        /// 传输后否需要校验文件
        /// </summary>
        private bool _needVerify= true;

        [XmlElementAttribute("_needVerify", IsNullable = false)]
        public bool NeedVerify
        {
            get { return _needVerify; }
            set { _needVerify = value; }
        }

        /// <summary>
        /// 文件复制选项
        /// </summary>
        private CopyOptions _copyOption = CopyOptions.Outcome;
        [XmlElementAttribute("copyOption", IsNullable = false)]
        public CopyOptions CopyOption
        {
            get { return _copyOption; }
            set { _copyOption = value; }
        }

        /// <summary>
        /// 目录格式
        /// </summary>
        private DirectoryFormat _dirFormat = DirectoryFormat.None;
        [XmlElementAttribute("dirFormat", IsNullable = false)]
        public DirectoryFormat DirFormat
        {
            get { return _dirFormat; }
            set { _dirFormat = value; }
        }

        /// <summary>
        /// 传输加密类型
        /// </summary>
        private FTPEncryptionMode _encryptMode = FTPEncryptionMode.SSL;
        [XmlElementAttribute("encryptMode", IsNullable = false)]
        public FTPEncryptionMode EncryptionMode
        {
            get { return _encryptMode; }
            set { _encryptMode = value; }
        }

        /// <summary>
        /// 形成密文，用于保存
        /// </summary>
        public void MakeCiphertext(string text)
        {
            if (!string.IsNullOrEmpty(text))
            {
                _ciphertext = CustomEncrypt.Encrypt(text);
            }
        }

        /// <summary>
        /// 获得明文
        /// </summary>
        public string GetPassword()
        {
            string password = "";
            if (!string.IsNullOrEmpty(_ciphertext))
            {
                password = CustomEncrypt.Deciphering(_ciphertext);
            }
            return password;
        }

        public override bool Serialize()
        {
            return Serializer.SaveToXml(ConfigFilePath, this);
        }

        public override bool Load()
        {
            Log.WriteAsync("FTP配置文件路径:" + ConfigFilePath);
            if (!File.Exists(ConfigFilePath))
            {
                Log.WriteAsync("配置文件不存在");
                return false;
            }
            FTPOption obj = (FTPOption)Serializer.LoadFromXml(ConfigFilePath, typeof(FTPOption));
            if (obj != null)
            {
                this._ciphertext = obj._ciphertext;
                this._copyOption = obj._copyOption;
                this._dirFormat = obj._dirFormat;
                this._nVer = obj._nVer;
                this._encryptMode = obj._encryptMode;
                this._folder = obj._folder;
                this._ip = obj._ip;
                this._isNecessary = obj._isNecessary;
                this._needVerify = obj._needVerify;
                this._port = obj._port;
                this._user = obj._user;
                return true;
            }
            else
            {
                ErrorMessage = Serializer.Message;
                return false;
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("FTP账户选项: ");
            Action<string, object> FillString =
                (string field, object val) =>
                {
                    if (val != null)
                    {
                        builder.Append("[");
                        builder.Append(field);
                        builder.Append("]: ");
                        builder.Append(val.ToString() + "; ");
                    }                 
                };         
            FillString("IP", _ip);         
            FillString("端口号", _port);
            FillString("用户名", _user);
            FillString("密码", GetPassword());
            FillString("默认目录", _folder);
            FillString("传输加密类型", _encryptMode);
            FillString("拷贝文件对象", _copyOption);
            FillString("上传成功是否必要", _isNecessary);
            FillString("是否需要检验文件的完整性", _needVerify);
            FillString("目录格式", _dirFormat);
            return builder.ToString();
        }
    }
}
